/*
************************************************************************************************
*  File     : flash.h
*
*  Header for NXP LPC17xx Flash.
*
*  Author   : zhangchao
*  Version  : 1.0
*
*
************************************************************************************************
*/
#ifndef __FLASH_H__
#define __FLASH_H__

#define VERS       1           // Interface Version 1.01

#define UNKNOWN    0           // Unknown
#define ONCHIP     1           // On-chip Flash Memory
#define EXT8BIT    2           // External Flash Device on 8-bit  Bus
#define EXT16BIT   3           // External Flash Device on 16-bit Bus
#define EXT32BIT   4           // External Flash Device on 32-bit Bus
#define EXTSPI     5           // External Flash Device on SPI

#define SECTOR_NUM 512         // Max Number of Sector Items
#define PAGE_MAX   65536       // Max Page Size for Programming

extern uint32_t SystemCoreClock;     /*!< System Clock Frequency (Core Clock)  	*/

struct FlashSectors  {
	uint32_t   szSector;    // Sector Size in Bytes
	uint32_t AddrSector;    // Address of Sector
};

#define SECTOR_END 0xFFFFFFFF, 0xFFFFFFFF

struct FlashDevice  {
	uint16_t     Vers;    // Version Number and Architecture
	int8_t       DevName[128];    // Device Name and Description
	uint16_t  DevType;    // Device Type: ONCHIP, EXT8BIT, EXT16BIT, ...
	uint32_t    DevAdr;    // Default Device Start Address
	uint32_t     szDev;    // Total Size of Device
	uint32_t    szPage;    // Programming Page Size
	uint32_t       Res;    // Reserved for future Extension
	uint8_t  valEmpty;    // Content of Erased Memory
	
	uint32_t    toProg;    // Time Out of Program Page Function
	uint32_t   toErase;    // Time Out of Erase Sector Function
	
	struct FlashSectors sectors[SECTOR_NUM];
};

#define FLASH_DRV_VERS (0x0100+VERS)   // Driver Version, do not modify!

// Memory Mapping Control

#define MEMMAP     (*((__IO uint8_t *) 0x400FC040))

#define STACK_SIZE     64      // Stack Size

#define SET_VALID_CODE 1       // Set Valid User Code Signature
#define  __FLASH_512

#ifdef __FLASH_512
#define END_SECTOR     29
#endif
#ifdef __FLASH_256
#define END_SECTOR     21
#endif
#ifdef __FLASH_128
#define END_SECTOR     17
#endif
#ifdef __FLASH_64
#define END_SECTOR     15
#endif
#ifdef __FLASH_32
#define END_SECTOR     7
#endif
#ifdef __FLASH_24
#define END_SECTOR     5
#endif
#ifdef __FLASH_16
#define END_SECTOR     3
#endif
#ifdef __FLASH_8
#define END_SECTOR     1
#endif

struct sIAP {             // IAP Structure
	uint32_t cmd;           // Command
	uint32_t par[4];        // Parameters
	uint32_t stat;          // Status
	uint32_t res[4];        // Result
};


#define PREPARE_WRITE            50
#define CP_RAM_TO_FLASH          51
#define EARSE_SECTOR             52
#define BLANK_CHECK_SECTOR       53
#define READ_PART_ID             54
#define READ_BOOT_VERSION        55
#define READ_DEV_SER_NUM         58
#define FLASH_COMPARE            56
#define REINVOKE_ISP             57

// Flash Programming Functions
int32_t Flash_Init(uint32_t adr, uint32_t clk, uint32_t fnc);       // Initialize Flash                                 
int32_t Flash_UnInit(uint32_t fnc);                                 // De-initialize Flash
int32_t Flash_EraseChip(void);                                      // Erase complete Device
int32_t Flash_EraseSector(uint32_t adr);                            // Erase Sector Function
int32_t Flash_ProgramPage(uint32_t adr, uint32_t sz, uint8_t *buf); // Program Page Function

//do not use
uint32_t Flash_Verify(uint32_t adr, uint32_t sz, uint8_t *buf);     // Verify Function
int32_t Flash_BlankCheck(uint32_t adr, uint32_t sz, uint8_t pat);   // Blank Check

#endif	// end __FLASH_H__

